package com.android.pocketguru;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

public class AlarmHandler 
{
	public static void setPopupAlarm(Context context, long alarm_time, int alarm_requestCode)
	{
		setPopupAlarm(context, alarm_time, alarm_requestCode, false, false);
	}
	public static void setPopupAlarm(Context context, long alarm_time, int alarm_requestCode, boolean isRecurring)
	{
		setPopupAlarm(context, alarm_time, alarm_requestCode, isRecurring, false);
	}
	public static void setPopupAlarm(Context context, long alarm_time, int alarm_requestCode, boolean isRecurring, boolean asDialog)
	{
		//Intent intent = new Intent(context, PopupActivity.class);
		Intent intent = new Intent(context, AlarmReceiver.class);
		Bundle b = new Bundle();
		b.putBoolean(Constants.POPUP_RECURRING_KEY, isRecurring);
		b.putBoolean(Constants.POPUP_AS_DIALOG_KEY, asDialog);
		b.putInt(Constants.ALARM_REQUEST_CODE_KEY, alarm_requestCode);
		intent.putExtras(b);
		
		//PendingIntent operation = PendingIntent.getActivity(context, alarm_requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		PendingIntent operation = PendingIntent.getBroadcast(context, alarm_requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP  ,alarm_time  , operation);
	}
	
	
	public static void setRecurringAlarm(Context context, int alarm_requestCode)
	{
		setRecurringAlarm(context, alarm_requestCode, false);
	}
	public static void setRecurringAlarm(Context context, int alarm_requestCode, boolean asDialog)
	{
		SharedPreferences prefs = context.getSharedPreferences(Constants.SETTING_PREFERENCE_NAME_KEY, 0);
		/*
        // time
        int settings_h1= prefs.getInt(MainActivity.SETTING_HOURS_FROM_KEY, MainActivity.SETTING_HOURS_FROM_DEFAULT);
        int settings_m1= prefs.getInt(MainActivity.SETTING_MINUTES_FROM_KEY,  MainActivity.SETTING_MINUTES_FROM_DEFAULT);
        int settings_h2= prefs.getInt(MainActivity.SETTING_HOURS_TO_KEY,  MainActivity.SETTING_HOURS_TO_DEFAULT);
        int settings_m2= prefs.getInt(MainActivity.SETTING_MINUTES_TO_KEY,  MainActivity.SETTING_MINUTES_TO_DEFAULT);
        */
		
        // frequency
        int setting_frequency = prefs.getInt(Constants.SETTING_FREQUENCY_KEY, Constants.SETTING_FREQUENCY_DEFAULT);
        
        if (alarm_requestCode == Constants.ALARM_TEST_REQUEST_CODE)
            setting_frequency = 1;

		
        // TODO: make logic to set alarms from now or available time period start.  Alarms also should have randomized delay
        
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, setting_frequency);
        
        long alarm_time = c.getTimeInMillis();
        
        setPopupAlarm(context, alarm_time, alarm_requestCode, true, asDialog);
        
	}
	
	
	
    public static void stopAlarm(Context context, int alarm_requestCode) {
    	//Intent intent = new Intent(context, PopupActivity.class);
    	Intent intent = new Intent(context, AlarmReceiver.class);
	    //PendingIntent pIntent = PendingIntent.getActivity(context, alarm_requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    	PendingIntent pIntent = PendingIntent.getBroadcast(context, alarm_requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
	    
	    AlarmManager alarmMng = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
	    alarmMng.cancel(pIntent);
    }
}
